package org.scohen.juploadr.uploadapi.flickrrest.photosets;

import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import org.scohen.juploadr.app.PhotoSet;
import org.scohen.juploadr.uploadapi.CommunicationException;
import org.scohen.juploadr.uploadapi.ImageUploadApi;
import org.scohen.juploadr.uploadapi.ProtocolException;
import org.scohen.juploadr.uploadapi.abilities.Abilities;
import org.scohen.juploadr.uploadapi.abilities.OrganizeAbilityNotification;
import org.scohen.juploadr.uploadapi.flickrrest.DefaultFlickrHandler;
import org.scohen.juploadr.uploadapi.flickrrest.FlickrMethod;
import org.scohen.juploadr.uploadapi.flickrrest.RestFlickrApi;
import org.scohen.juploadr.uploadapi.flickrrest.UserNotFoundException;

public class GetPhotosetList extends FlickrMethod {
    private GetPhotosetListHandler handler = new GetPhotosetListHandler(this);

    public DefaultFlickrHandler getResponseHandler() {
        return handler;
    }

    public boolean execute() throws ProtocolException, CommunicationException {
        boolean success = super.execute();
        if (success) {
            ImageUploadApi.getInstance().getAbilities().getOrganizeAbilities().notified(
                    new OrganizeAbilityNotification(handler.getPhotoSets(),true));
        }
        return success;
    }

    public SortedMap<String, String> getParams() {
        SortedMap<String, String> params = new TreeMap<String, String>();
        params.put("method", "flickr.photosets.getList");
        params.put("api_key", RestFlickrApi.FLICKR_API_KEY);
        return params;
    }

    public List<PhotoSet> getPhotoSets() {
        return handler.getPhotoSets();
    }

    public boolean isAuthorized() {
        return true;
    }

    @Override
    protected ProtocolException buildProtocolException(int errorCode) {
        if (errorCode == 1) {
            return new UserNotFoundException();
        }
        return null;
    }

}
